package com.xing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xing.Utils.Result;
import com.xing.core.BaseServiceImpl;
import com.xing.dao.BroadcastDao;
import com.xing.dao.RoomDao;
import com.xing.dto.BroadcastDTO;
import com.xing.entity.BroadcastEntity;
import com.xing.entity.RoomEntity;
import com.xing.service.BroadcastService;
import com.xing.user.User;
import com.xing.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/4/27 22:03
 */
@Service
public class BroadcastServiceImpl extends BaseServiceImpl<BroadcastDao, BroadcastEntity> implements BroadcastService {
    @Autowired
    private BroadcastDao dao;
    @Autowired
    private RoomDao roomDao;

    @Override
    public PageData<BroadcastDTO> getLiveHistory(Integer page, Integer limit) {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<BroadcastDTO> broadcastByUserId = dao.getBroadcastByUserId(principal.getId(), (page - 1) * limit, limit);
        Integer number = dao.getLiveNumber(principal.getId());
        return new PageData<>(broadcastByUserId, number);
    }

    @Override
    public Boolean DeleteLiveHistory(Long id) {
        try {
            BroadcastEntity byId = getById(id);
            byId.setDelFlag(1);
            boolean b = saveOrUpdate(byId);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Result beginLive() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        BroadcastEntity broadcastEntity = new BroadcastEntity();
        QueryWrapper<RoomEntity> roomEntityQueryWrapper = new QueryWrapper<>();
        roomEntityQueryWrapper.eq("anchor_id", principal.getId());
        RoomEntity roomEntity = roomDao.selectOne(roomEntityQueryWrapper);
        broadcastEntity.setRoomId(roomEntity.getRoomNumber());
        broadcastEntity.setStartDate(new Date());
        boolean save = save(broadcastEntity);
        if (save) {
            return new Result().ok();
        }
        return new Result().err(501, "添加失败");
    }

    @Override
    public Result endLive() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        QueryWrapper<RoomEntity> roomEntityQueryWrapper = new QueryWrapper<>();
        roomEntityQueryWrapper.eq("anchor_id", principal.getId());
        RoomEntity roomEntity = roomDao.selectOne(roomEntityQueryWrapper);
        QueryWrapper<BroadcastEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomEntity.getRoomNumber())
                .isNull("end_date");
        BroadcastEntity broadcastEntity = dao.selectOne(wrapper);
        if (broadcastEntity != null) {
            broadcastEntity.setEndDate(new Date());
            boolean updateById = updateById(broadcastEntity);
            if (updateById) {
                return new Result().ok();
            }
            return new Result().err(501, "修改失败");
        }
        return new Result().ok();
    }
}
